/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution.

    Maxsi Distribution is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Distribution. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionContributors.h. For more information please see
	MaxsiDistributionContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistribution.dll
	Distributing since 2008

	Thread.h
	Include file for all the threads in the MaxsiInstaller.exe

****************************************************************************/

#ifndef Thread_H
#define Thread_H

#include "MaxsiTorrent.h"

#define MSG_Next			100
#define MSG_Previous		101
#define MSG_Cancel			102
#define MSG_Finish			103
#define MSG_SetStatus		104
#define	MSG_SetProgress		105
#define	MSG_RecieveError	106
#define	MSG_RecieveNews		107
#define MSG_SetSubStatus	108
#define GotoFailurePage		200
#define GotoSuccessPage		201
#define GotoConnectionPage	202
#define GotoErrorPage		203
#define GotoPasswordPage	204
#define QuitMessage			300

// The thread that installs the product!
class	ClassThreadInstall : public ME_Thread
{
	Declare_Class(ThreadInstall,ME_Thread);

public:
	virtual		LRESULT APIENTRY	WindowProc			(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	virtual		BOOL				InitApplication		(HANDLE hInstance);
	virtual		BOOL				InitInstance		(HINSTANCE hInstance, INT nCmdShow);
	virtual		LRESULT				ME_Thread_Cmd		(HWND hWnd, WPARAM wParam, LPARAM lParam);

public:
	bool				ProductRegistered;
	ME_TorrentServer*	TorrentServer;	
};

ME_THREAD_LINK_H(ThreadInstall,ClassThreadInstall,MDLINKCPP);

// The thread that gets information about the product, before it's installed.
class	ClassThreadInitialize : public ME_Thread
{
	Declare_Class(ClassThreadInitialize,ME_Thread);

public:
	virtual		LRESULT APIENTRY	WindowProc			(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	virtual		BOOL				InitApplication		(HANDLE hInstance);
	virtual		BOOL				InitInstance		(HINSTANCE hInstance, INT nCmdShow);

public:
	// Todo.
};

ME_THREAD_LINK_H(ThreadInitialize,ClassThreadInitialize,MDLINKCPP);

MISFUNCH(Server);
MISFUNCH(File);
MISFUNCH(BlockHash);
MISFUNCH(SuccessfullyRegistered);

int		InstallProduct		();
bool	Error				(const char* Error);
bool	NewsUpdated			(const char* News);
bool	Info				(const char* Error);
bool	SetStatus			(const char* Status);
bool	SetSubStatus		(const char* Status);
bool	SetProgress			(int Progress);
bool	ShowFailPage		();
bool	ShowConnectionPage	();
bool	ShowErrorPage		(char* Error);
bool	ShowPasswordPage	();
bool	SendQuitMessage		();
bool	DownloadNewsFunc	(char* Host, char* Resource, char* Product, char* Format);
bool	OnSpeedUpdate		(ME_TorrentServer* Updated, size_t	TorrentId);
bool	OnBufferExpanded	(ME_FileServer* Updated);
bool	GenerateTorrentData	();
bool	CheckFileStateReliable(MaxsiTorrentFile* File, MaxsiTorrentData* Data);
bool	CheckBlockStateReliable(MaxsiTorrentBlock* Block, char* Buffer);
bool	CheckBlockStatesReliable(MaxsiTorrentData* Data);
bool	CheckBlockStatesFast(MaxsiTorrentData* Data);
bool	WaitForPIDToClose();
bool	Register			(char* ProductName, char* MasterServer, char* SteamUserName, char* Password, ClassThreadInstall* l_ClassThreadInstall);
bool	RunInstallScript	(char* ContentArchive, char* MasterServer, char* SteamUserName, ME_TorrentServer* dTorrent, ClassThreadInstall* l_ClassThreadInstall, size_t J);
bool	OutputContentArchiveState(MaxsiTorrentData* Data);

bool	InitializeProduct();


DECLARE_EXTERN_FUNCTION(bool, ErrorFunc,		(const char* Error));//		=	(type_ErrorFunc)		&Error;
DECLARE_EXTERN_FUNCTION(bool, InfoFunc,			(const char* Error));//		=	(type_InfoFunc)			&Info;
DECLARE_EXTERN_FUNCTION(bool, NewsUpdatedFunc,	(const char* News));//			=	(type_NewsUpdatedFunc)	&NewsUpdated;
DECLARE_EXTERN_FUNCTION(bool, StatusFunc,		(const char* Status));//		=	(type_StatusFunc)		&SetStatus;
DECLARE_EXTERN_FUNCTION(bool, SubStatusFunc,	(const char* Status));//		=	(type_SubStatusFunc)	&SetSubStatus;
DECLARE_EXTERN_FUNCTION(bool, ProgressFunc,		(int Progress));//				=	(type_ProgressFunc)		&SetProgress;
DECLARE_EXTERN_FUNCTION(bool, CheckBlockStates,	(MaxsiTorrentData* Data));//	=	(type_CheckBlockStates)	&CheckBlockStatesReliable;
DECLARE_EXTERN_FUNCTION(bool, DownloadFunc,		());//						=	0;
DECLARE_EXTERN_FUNCTION(bool, NoInternet,		());//						=	0;
DECLARE_EXTERN_FUNCTION(bool, NeedsUpdating,	());//						=	0;
DECLARE_EXTERN_FUNCTION(bool, DownloadNews,		(char* Host, char* Resource, char* Product, char* Format));//				=	(type_DownloadNews)		&DownloadNewsFunc;


#endif